<template>
  <a-modal
    :title="$t('all.autotext1793')"
    :visible="visible"
    @cancel="handleCancel"
    @ok="handleOk"
  >
    <a-form-model ref="formRef" :model="form" v-bind="formItemLayout">
      <a-form-model-item v-if="type === 2" :label="$t('all.autotext1718')">
        {{ form.businessCode }}
      </a-form-model-item>
      <a-form-model-item v-else :label="$t('all.autotext1718')" prop="businessCode">
        <a-select
          v-model="form.businessCode"
          show-search
          :filter-option="filterOption"
          allowClear
        >
          <a-select-option v-for="list in codeList" :key="list">
            {{ list }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item :label="$t('all.autotext1794')">
        <a-range-picker :disabled-date="disabledDate" v-model="rangTime" />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
<script>
import moment from 'moment'
import { exportContractInfo, getMyBusinessCode } from '@/api/entry/entry'
import { exportPdf, exportExcel } from '@/utils/util'
export default {
  props: {
    format: {
      type: String,
      default: 'excel'
    }
  },
  data() {
    return {
      formItemLayout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 15 }
      },
      visible: false,
      type: '',
      form: {
        businessCode: ''
      },
      codeList: [],
      rangTime: []
    }
  },
  methods: {
    open(type, code) {
      this.type = type
      this.form.businessCode = code || undefined
      this.visible = true
      if (!code) {
        this.getBusinessCode()
      }
    },
    // 查询合同号列表
    async getBusinessCode() {
      const res = await getMyBusinessCode()
      this.codeList = res || []
    },
    disabledDate(current) {
      return current && current > moment().subtract(0, 'day').endOf('day')
    },
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      )
    },
    async handleOk() {
      const res = await exportContractInfo({
        businessCode: this.form.businessCode,
        startDateString: this.rangTime.length
          ? moment(this.rangTime[0]).format('YYYY-MM-DD') + ' 00:00:00'
          : undefined,
        endDateString: this.rangTime.length
          ? moment(this.rangTime[1]).format('YYYY-MM-DD') + ' 00:00:00'
          : undefined,
        classify: this.$store.state.user.classify,
        format: this.format
      })
      let time = this.rangTime.length
        ? ` (${moment(this.rangTime[0]).format('YYYY.MM.DD')}-${moment(
          this.rangTime[1]
        ).format('YYYY.MM.DD')})`
        : ''
      let title = `${this.form.businessCode}${this.$t(
        'all.autotext1803'
      )}${time}`
      if (this.format === 'pdf') {
        exportPdf(title, res.data, true)
      } else {
        exportExcel(title, res.data, true)
      }
    },
    handleCancel() {
      this.$refs.formRef.resetFields()
      this.rangTime = []
      this.visible = false
    }
  }
}
</script>
